﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP09S03_OfficeTracking
{
    public partial class FrmSupplies : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        public FrmSupplies()
        {
            InitializeComponent();

        ctrlItemSearch1.btnSearch.Click +=new EventHandler(btnSearch_Click);
        }

        public void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                dgvItem.EndEdit();
                this.officeItemsTableAdapter.Update(this.dSItem.OfficeItems );
            }
            catch (Exception ex)
            {
                CatchError(ex);
            }

            try
            {
                DiagAddSupplies d = new DiagAddSupplies();
                d.ShowDialog();
                this.dSItem.OfficeItems.Clear();
                this.officeItemsTableAdapter.FillByConsumable(this.dSItem.OfficeItems ,true  );
                updateTotal();

            }
            catch (Exception ex)
            {
                CatchError(ex);
            }
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {

            try
            {
                this.dSItem.OfficeItems.Clear();

                this.officeItemsTableAdapter.FillBySearch(this.dSItem.OfficeItems, ctrlItemSearch1.TEXTBOX_ID.Text, ctrlItemSearch1.TEXTBOX_Name.Text, (int)ctrlItemSearch1.ComboBox_Category.SelectedValue, ctrlItemSearch1.TEXTBOX_Brand.Text, ctrlItemSearch1.TEXTBOX_Model.Text  ,true );

                updateTotal();
            }
            catch (Exception ex)
            {

                CatchError(ex);
            }

 

        }


        private void updateTotal()
        {
            SetMessage("Total " + this.dSItem.OfficeItems.Count.ToString() + " Records!");

        }

        private void FrmSupplies_Load(object sender, EventArgs e)
        {
            try
            {
                this.categoryTableAdapter.Fill(this.dSItem.Category);
                this.officeItemsTableAdapter.FillByConsumable(this.dSItem.OfficeItems, true);
            }
            catch (Exception ex) {
                CatchError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                dgvItem.EndEdit();
                this.officeItemsTableAdapter.Update(this.dSItem.OfficeItems);
                SetMessage("Update Successfully!");
            }
            catch (Exception ex)
            {
                CatchError(ex);
            }
        }

        private void dgvItem_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            updateTotal();
            if (e.RowIndex > -1)
            {
                btnDelete.Enabled = true;

            }
            else
            {
                btnDelete.Enabled = false;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dgvItem.CurrentRow;

            if (row == null)
            {
                MessageBox.Show("No Record Seletectd");
                return;
            }

            int con = 0;

            if (row.IsNewRow == false)
            {
                //check items belong to category.

                Controller.ItemController ic = new Controller.ItemController();

                con = ic.CheckItemID(row.Cells[0].Value.ToString());


            }


            if (con > 0)
            {

                CatchError(new Exception("Cannot delete this one because this one already has some associate transaction !"));

            }
            else
            {
                if (ShowDeleteConfirm() == DialogResult.Yes)
                {
                    dgvItem.Rows.Remove(row);
                    updateTotal();
                }
            }
        }

        private void dgvItem_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

            e.Cancel = true;
            CatchError(new Exception("Invalid Data entered!"));
           
        }

     

    }
}
